﻿namespace CN2P.Web.Controllers.Api
{
    using System;
    using System.Configuration;
    using System.Net;
    using System.Net.Http;
    using System.Text;
    using System.Web.Http;

    public class BaseApiController : ApiController
    {
        protected HttpResponseMessage NotFound(string code, string description)
        {
            var error = new HttpError();
            error["code"] = code;
            error["description"] = description;
            error.Message = description;
            return Request.CreateErrorResponse(HttpStatusCode.NotFound, error);
        }

        protected HttpResponseMessage BadRequest(string code, string description)
        {
            var error = new HttpError();
            error["code"] = code;
            error["description"] = description;
            error.Message = description;
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, error);
        }

        protected string DecodeUserId(string userId)
        {
            return Encoding.UTF8.GetString(Convert.FromBase64String(userId));
        }

        protected string ProductsUri
        {
            get { return ConfigurationManager.AppSettings["ProductsUri"]; }
        }

        protected string GetProductUri(string prodId)
        {
            return string.Format("{0}api/product/{1}/", ProductsUri, prodId);
        }

        protected string GetProductReviewUrl(string prodId, string reviewId)
        {
            return string.Format("{0}/reviews/{1}", GetProductUri(prodId), reviewId);
        }
    }
}